<?php
include_once 'entity/ProductType.php';
class Administrator_ProductType_Grid extends BaseGrid{
	protected $seachEntity = array(
		ProductType::ID,
		ProductType::NAME
	);
	
	public function init(){
		$this->headerLb[ProductType::ID] = 'ID'; 
		$this->headerLb[ProductType::NAME] = 'Tên';
		$this->headerLb[ProductType::DESCRIPTION] = 'Mô tả';
		$this->headerLb[ProductType::IMAGE] = 'Hình ảnh';
	}
	public function _preRender(){
		$link = $this->getEditLink();
		$this->cols = array(
			BaseGrid::COL_ORDINAL => array(BaseGrid::TYPE_ORDINAL),
			BaseGrid::COL_CHECKBOX => array(BaseGrid::TYPE_CHECKBOX),
			ProductType::NAME => array(ProductType::NAME,BaseGrid::TYPE_LINK,$link,array(ProductType::ID)),
			ProductType::LASTUPDATEBY => array(ProductType::LASTUPDATENAME,BaseGrid::TYPE_TEXT),
			ProductType::LASTUPDATETIME => array(ProductType::LASTUPDATETIME,BaseGrid::TYPE_TEXT),
			ProductType::ROWVERSION => array(ProductType::ROWVERSION,BaseGrid::TYPE_TEXT),
			ProductType::STATUS => array(ProductType::STATUSNAME,BaseGrid::TYPE_TEXT),
			ProductType::ID => array(ProductType::ID,BaseGrid::TYPE_TEXT)
		);
	}
	public function searchQuery(){
		$sql = 'SELECT t.' .ProductType::ID.',';
		$sql .= 't.' .ProductType::NAME.',';
		$sql .= 't.' .ProductType::IMAGE.',';
		$sql .= 't.' .ProductType::LASTUPDATETIME.',';
		$sql .= 't.' .ProductType::ROWVERSION.',';
		$sql .= 'u.name as '.ProductType::LASTUPDATENAME.',';
		$sql .= 's.name as '.ProductType::STATUSNAME;
		$sql .= ' FROM producttype as t';
		
		$sql .= ' LEFT JOIN usersystem as u';
		$sql .= ' ON t.lastupdateby = u.id';
		
		$sql .= ' LEFT JOIN status as s';
		$sql .= ' ON t.status = s.id';
		
		$where = " WHERE ";
		
		if(isset($this->searchCondition['ProductType_id']) && $this->searchCondition['ProductType_id'] != "")
		$where .= ' t.id="'.$this->searchCondition['ProductType_id'].'" and';
		
		if(isset($this->searchCondition['ProductType_name']) && $this->searchCondition['ProductType_name'] != "")
		$where .= ' t.name like"%'.$this->searchCondition['ProductType_name'].'%" and';
		
		if(isset($this->searchCondition['ProductType_status']) && $this->searchCondition['ProductType_status'] != "")
		$where .= ' t.status="'.$this->searchCondition['ProductType_status'].'" and';
		
		$where .= ' 1 = 1';
		$sql .= $where;
		return $sql;
	}
}